/**
 * @classDescription  模拟选择框类
 * @type {Object}
 * @param {String | Object} 触发弹出层对象
 * @param {String | Object}  弹出层对象
 * @param {String} 弹出层列表标签 
 * @param {String | Object}  返回值输入对象
 * @param {Object}  参数集合（包括writeInfo,onkeypress,callback）
 */ 
function fakeSelect(touchId, selectId, selectTag, setInId, option){
	this.setOption(option);	
	this.touch = Kg.$(touchId);
	this.select = Kg.$(selectId);
	this.set = Kg.$(setInId);
	this.li = Kg.$T(selectTag, this.select);
	this.currentIndex = -1;
	Kg.UA.Ie?null:(this.touch.type = "text");
	var _this = this;

	var func = document.body.onclick;
	document.body.onclick = function(){
		func && func();
		if(_this.select.offsetHeight > 0){
			_this.select.style.display = "none";
		}
	};
	
	Kg.addEvent(this.touch, "click", Kg.bind(function(event){
		Kg.stopEvent(event);
		if(this.select.offsetHeight <= 0 && this.li.length > 0){
			this.select.style.display = "block";
		}
	},this));

	Kg.addEvent(this.touch, "keydown", Kg.bind(function(event){
		var e = event || window.event;

		if(e.keyCode !== 37 && e.keyCode !== 38 && e.keyCode !== 39 && e.keyCode !== 40 && e.keyCode !== 13){
			this.option.onkeypress && this.option.onkeypress(this);
		} else {
			var l = this.li.length;

			if(e.keyCode == 13){
				this.close();
				this.option.callback && this.option.callback(this);
			}

			this.li[this.currentIndex] && (this.li[this.currentIndex].className = this.li[this.currentIndex].className.replace(/(\s*current)*/g,""));
			if(e.keyCode == 38){	
				this.currentIndex = (this.currentIndex <= 0)?l - 1:--this.currentIndex;
				this.li[this.currentIndex] && (this.li[this.currentIndex].className += " current");
				this.option.writeInfo === "mouseover" && this.write();
			}
			if(e.keyCode == 40){
				this.currentIndex = (this.currentIndex >= l-1)?0:++this.currentIndex;
				this.li[this.currentIndex] && (this.li[this.currentIndex].className += " current");
				this.option.writeInfo === "mouseover" && this.write();
			}
		}
	}, this));

	this.bindLi();
};

fakeSelect.prototype.bindLi = function(){
	for(var i = 0; i < this.li.length; i++){
		Kg.addEvent(this.li[i], "mouseover", Kg.bind(function(i){
			this.li[this.currentIndex] && (this.li[this.currentIndex].className = this.li[this.currentIndex].className.replace(/(\s*current)*/g,""));
			this.li[i].className += " current";
			this.currentIndex = i;
		}, this, i));
		Kg.addEvent(this.li[i], "mouseout", Kg.bind(function(){
			this.className = this.className.replace(/(\s*current)*/g,"");
		}, this.li[i]));
		Kg.addEvent(this.li[i], "click", Kg.bind(function(){
			this.close();
			this.option.callback && this.option.callback(this);
		}, this));
	}
};

fakeSelect.prototype.setOption = function(option){
	this.option = {
		writeInfo:"click",	//按下回车键才输入值
		onkeypress:null,
		callback:null
	};

	Kg.extend(this.option, option || {}, true);
};

fakeSelect.prototype.write = function(){
	var selected = Kg.$C("current",this.select);
	if(selected.length > 0){
		this.touch.value = selected[0].innerHTML.trim();
		this.set.tagName.toLowerCase() === "input"?
			this.set.value = this.touch.value:
			this.set.innerHTML = this.touch.value;
	}
};

fakeSelect.prototype.close = function(){
	this.write();
	this.select.style.display = "none";
};

fakeSelect.prototype.open = function(event){
	Kg.stopEvent(event);
	this.touch.focus();
	if(Kg.UA.Ie){
		this.touch.click();
	} else {
		var e = document.createEvent("MouseEvent");
		e.initEvent("click",true,true);
		this.touch.dispatchEvent(e);
	}	
};

fakeSelect.email = {
	emails:[],
	func:function(o){
		setTimeout(function(){
			var reg = /\w+(\.\w+){1,2}$/;
			var text = o.set.value;
			var el = Kg.$("email_div");
			var str = "";
			var emails = fakeSelect.email.emails;
			var index = text.indexOf("@");
			var prefix = "";
			if(index >= 0){
				prefix = text.substr(index+1);
				for(var i = 0; i < emails.length; i++){
					if(new RegExp("^" + prefix).test(emails[i]))
						str += "<p>" + text + (emails[i].replace(prefix,"")) + "</p>";
				}
			} else {
				for(var i = 0; i < emails.length; i++){
					str += "<p>" + text + "@" + emails[i] + "</p>";
				}
			}
			
			el.innerHTML =  str;

			o.li = Kg.$T("p", el);
			o.currentIndex = -1;
			o.bindLi();
			if(o.li.length > 0){
				o.select.style.display = "block";
			}else{
				o.select.style.display = "none";
			}
		});
	}
}